package com.fang.networkdisk.controller;

import com.fang.common.common.CommonPage;
import com.fang.common.common.CommonResult;
import com.fang.common.request.BaseController;
import com.fang.common.util.CommonResultUtil;
import com.fang.networkdisk.entity.bo.PermissionBo;
import com.fang.networkdisk.entity.dto.PermissionDto;
import com.fang.networkdisk.entity.vo.PermissionVo;
import com.fang.networkdisk.service.TbNetDisPermissionService;
import com.fang.networkdisk.util.NDBeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Bernie_fang
 * @Description:
 * @create 2025/1/30 17:05
 **/
@RestController
@RequestMapping("/system/permission")
public class PermissionController extends BaseController {

    @Resource
    private TbNetDisPermissionService tbNetDisPermissionService;

    @PostMapping("/list")
    public CommonResult<CommonPage<List<PermissionVo>>> queryPermission(@RequestBody PermissionDto permissionDto) {
        PermissionBo permissionBo = new PermissionBo();
        NDBeanUtils.copyListProperties(permissionDto, permissionBo);
        CommonPage result = tbNetDisPermissionService.queryPermission(permissionBo);
        return CommonResultUtil.success(result);
    }

    @PostMapping("/add")
    public CommonResult<CommonPage<List<PermissionVo>>> addPermission(@RequestBody PermissionDto permissionDto) {
        String authorId = super.getAuthorId();
        PermissionBo permissionBo = new PermissionBo();
        NDBeanUtils.copyListProperties(permissionDto, permissionBo);
        permissionBo.setCreateUser(authorId);
        permissionBo.setUpdateUser(authorId);
        tbNetDisPermissionService.addPermissions(permissionBo);
        return CommonResultUtil.success();
    }

    @PostMapping("/update")
    public CommonResult<CommonPage<List<PermissionVo>>> updatePermission(@RequestBody PermissionDto permissionDto) {
        String authorId = super.getAuthorId();
        PermissionBo permissionBo = new PermissionBo();
        NDBeanUtils.copyListProperties(permissionDto, permissionBo);
        permissionBo.setCreateUser(null);
        permissionBo.setUpdateUser(authorId);
        tbNetDisPermissionService.updatePermissionsById(permissionBo);
        return CommonResultUtil.success();
    }

    @GetMapping("/delete/{id}")
    public CommonResult<CommonPage<List<PermissionVo>>> deletePermission(@PathVariable("id") Integer id) {
        String authorId = super.getAuthorId();
        tbNetDisPermissionService.deletePermissionsById(id, authorId);
        return CommonResultUtil.success();
    }

}
